package com.imooc.conver;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/v1/api/user")
@RestController
public class UserApi {

    @Autowired
    private UserService userService;

    public User addUser(UserInputDTO userInputDTO) {
        User user = convertFor(userInputDTO);

        return userService.addUser(user);
    }

    private User convertFor(UserInputDTO userInputDTO) {
        User user = new User();
        User zs = User.builder().userName("zs").age(24).build();
        BeanUtils.copyProperties(userInputDTO, User.class);
        return user;
    }

    static List<User> childCategoryList = new ArrayList<>();


    public static void main(String[] args) {
        List<User> allCategoryList = new ArrayList<>();
        User user1 = new User();
        user1.setAuId(0);
        user1.setParentId(-1);

        User user2 = new User();
        user2.setAuId(1);
        user2.setParentId(0);

        User user3 = new User();
        user3.setAuId(2);
        user3.setParentId(1);

        allCategoryList.add(user1);
        allCategoryList.add(user2);
        allCategoryList.add(user3);
        System.out.println(allCategoryList);
        String categoryAuId = "0";
        childCategoryList = new ArrayList<User>();
        List<User> rMap = getChildCategory(allCategoryList, categoryAuId);
        System.out.println("rMap=" + rMap);
    }

    public static List<User> getChildCategory(List<User> allCategoryList, String parentId) {
        String str = "";
        for (User category : allCategoryList) {
            // 判断是否存在子节点
            if (category.getParentId().toString().equals(parentId)) {
                // 递归遍历下一级
                getChildCategory(allCategoryList, category.getAuId().toString());
                childCategoryList.add(category);
            }
            str = str + ">" + category.getParentId().toString();
        }
        System.out.println("childCategoryList=" + childCategoryList);
        System.out.println(str);
        return childCategoryList;
    }

//    public static void main(String[] args) {
//        List<Map<String, Object>> allCategoryList = new ArrayList<Map<String,Object>>();
//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("auId", "0");
//        map.put("parentId", "-1");
//        Map<String, Object> map1 = new HashMap<String, Object>();
//        map1.put("auId", "1");
//        map1.put("parentId", "0");
//        Map<String, Object> map2 = new HashMap<String, Object>();
//        map2.put("auId", "2");
//        map2.put("parentId", "1");
//        Map<String, Object> map3 = new HashMap<String, Object>();
//        map3.put("auId", "3");
//        map3.put("parentId", "2");
//        Map<String, Object> map4 = new HashMap<String, Object>();
//        map4.put("auId", "4");
//        map4.put("parentId", "3");
//        Map<String, Object> map5  = new HashMap<String, Object>();
//        map5.put("auId", "44");
//        map5.put("parentId", "3");
//        Map<String, Object> map6  = new HashMap<String, Object>();
//        map6.put("auId", "55");
//        map6.put("parentId", "4");
//        allCategoryList.add(map);
//        allCategoryList.add(map1);
//        allCategoryList.add(map2);
//        allCategoryList.add(map3);
//        allCategoryList.add(map4);
//        allCategoryList.add(map5);
//        allCategoryList.add(map6);
//        String categoryAuId = "0";
//        childCategoryList = new ArrayList<Map<String,Object>>();
//        List<Map<String, Object>> rMap = getChildCategory(allCategoryList, categoryAuId);
//        System.out.println("rMap=" + rMap);
//    }
//
//    public static List<Map<String, Object>> getChildCategory(List<Map<String, Object>> allCategoryList, String parentId){
//        for (Map<String, Object> category : allCategoryList) {
//            // 判断是否存在子节点
//            if (category.get("parentId").toString().equals(parentId)) {
//                // 递归遍历下一级
//                getChildCategory(allCategoryList, category.get("auId").toString());
//                childCategoryList.add(category);
//            }
//        }
//        System.out.println("childCategoryList=" + childCategoryList);
//        return childCategoryList;
//    }

}
